package com.kly.user.db.dao.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dao.UserWlmDao;
import com.kly.user.db.dbo.UserAfDBO;
import com.kly.user.db.dbo.FilterOnlineUserCustomerDBO;
import com.kly.user.db.dbo.ModelUserSendMsgNumDBO;
import com.kly.user.db.dbo.UserWlmDBO;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.db.entity.UserWlmEntity;
import com.kly.user.enums.LikeEnum;
import com.kly.user.enums.OnlineStatus;
import com.kly.user.enums.UserType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * User: linqu
 * Date: 2023-05-24
 * Time: 10:10:36.922
 */

@Repository
public class UserWlmDaoImpl extends AbstractBaseMapper<UserWlmEntity> implements UserWlmDao {

    @Override
    public List<UserWlmEntity> selectByUserCode(String userCode) {
        return getSqlSession().selectList(this.getStatement("selectByUserCode"), userCode);
    }

    @Override
    public List<UserWlmEntity> selectByWlmUserCode(UserWlmEntity userWlmEntity) {
        return getSqlSession().selectList(this.getStatement("selectByWlmUserCode"), userWlmEntity);
    }

    @Override
    public List<UserWlmEntity> queryUserRelationByFriendUserCode(UserWlmEntity entity) {
        return this.getSqlSession().selectList(this.getStatement("queryUserRelationByFriendUserCode"), entity);
    }

    @Override
    public List<UserWlmEntity> wlmList(String userCodeWlm, Page page) {
//        page.setTotalCount(this.queryTotalWlmCount2(userCodeWlm));
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCodeWlm", userCodeWlm);
        params.put("deleted", 0);
        params.put("offset", page.getStartIndex());
        params.put("limit", page.getPageSize());


        return this.getSqlSession().selectList(this.getStatement("wlmList"), params);
    }

    @Override
    public List<UserWlmEntity> queryOnlineWlmList(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryOnlineWlmList"), params);
    }

    public int queryTotalWlmCount2(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCodeWlm", userCode);
        params.put("deleted", 0);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryTotalWlmCount2"), params);
    }

    @Override
    public int queryUserRelationNum(UserWlmEntity entity) {
        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryUserRelationNum"), entity);
    }

    @Override
    public List<UserWlmDBO> needSendFlashMsgList(Integer limitNum) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("limitNum", limitNum);
        return this.getSqlSession().selectList(this.getStatement("needSendFlashMsgList"), params);
    }

    @Override
    public int removeByUserCode(String userCode) {
        return this.getSqlSession().delete(this.getStatement("removeByUserCode"), userCode);
    }

    @Override
    public List<UserWlmEntity> queryLikeEachOtherList(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("deleted", 0);
        return this.getSqlSession().selectList(this.getStatement("queryLikeEachOtherList"), params);

    }

    @Override
    public List<UserWlmEntity> queryOnlineMatchedList(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("queryOnlineMatchedList"), params);
    }

    @Override
    public Integer doAction(String userCode, String userCodeFriend, Integer op, String reason) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeFriend", userCodeFriend);
        if (Objects.nonNull(op)) {
            params.put("tagLableType", op);
        }
        if (StringUtils.isNotBlank(reason)) {
            params.put("reason", reason);
        }

        return this.getSqlSession().update(this.getStatement("doAction"), params);
    }

    @Override
    public List<UserWlmEntity> queryLoseEffectiveRecords(int likeType, Date date) {

        Map<String, Object> params = Maps.newHashMap();
        params.put("deleted", 0);
        params.put("wlmType", likeType);
        params.put("effectiveDate", date);

        return this.getSqlSession().selectList(this.getStatement("queryLoseEffectiveRecords"), params);
    }

    @Override
    public int queryLoseEffectiveTotal(int likeType, Date date) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("deleted", 0);
        params.put("wlmType", likeType);
        params.put("effectiveDate", date);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryLoseEffectiveTotal"), params);
    }

    @Override
    public int reBackToStranger(int likeType, Date date) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wlmType", likeType);
        params.put("effectiveDate", date);
        return this.getSqlSession().update(this.getStatement("reBackToStranger"), params);
    }

    @Override
    public int updateFlashChatRecord(String userCode, String userCodeWlm) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeWlm", userCodeWlm);
        return this.getSqlSession().update(this.getStatement("updateFlashChatRecord"), params);
    }

    @Override
    public int likesBack(String userCode, String userCodeWlm) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCodeWlm", userCodeWlm);
        params.put("userCode", userCode);
        return this.getSqlSession().update(this.getStatement("likesBack"), params);
    }

    /**
     * 用于距离上次下发N分钟的wlm数量查询
     */
    @Override
    public int receivedWlmNum(String userCode, LikeEnum likeType, OnlineStatus onlineStatus, int minutes) {
        Map<String, Object> params = Maps.newHashMap();
        if (Objects.nonNull(likeType)) {
            params.put("wlmType", likeType.getType());
        }
        if (Objects.nonNull(onlineStatus)) {
            params.put("online", onlineStatus.getType());
        }
        params.put("deleted", 0);
        params.put("userCodeWlm", userCode);
        params.put("sendWlmTime", DateUtil.offsetMinute(DateUtil.date(), minutes));
        return (Integer) this.getSqlSession().selectOne(this.getStatement("receivedWlmNum"), params);
    }

    /**
     * 用于指定时间后的下发wlm数量查询
     */
    @Override
    public int receivedWlmNum(String userCode, int likeType, DateTime dateTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wlmType", likeType);
        params.put("deleted", 0);
        params.put("userCodeWlm", userCode);
        params.put("sendWlmTime", dateTime);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("receivedWlmNum"), params);
    }

    /**
     * 用于指定时间后的用户Like记录
     */
    @Override
    public int userLike(String userCode, int likeType, DateTime dateTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wlmType", likeType);
        params.put("deleted", 0);
        params.put("userCode", userCode);
        params.put("likesTime", dateTime);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("userLike"), params);
    }

    /**
     * 用于当天建联查询
     */
    @Override
    public int queryContactNumToday(String userCode, String userCodeWlm, LikeEnum likeType) {
        Map<String, Object> params = Maps.newHashMap();
        if (Objects.nonNull(likeType)) {
            params.put("wlmType", likeType.getType());
        }
        params.put("deleted", 0);
        if (com.fqgj.common.utils.StringUtils.isNotEmpty(userCode)) {
            params.put("userCode", userCode);
        }
        if (StringUtils.isNotBlank(userCodeWlm)) {
            params.put("userCodeWlm", userCode);
        }
        params.put("contactTime", DateUtil.beginOfDay(DateUtil.date()));

        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryContactNumToday"), params);

    }

    @Resource
    UserDao userDao;

    @Override
    public List<UserWlmEntity> queryLikeEachOtherList(String userCode, Page page) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("deleted", 0);
        if (page != null) {
            page.setTotalCount(this.queryLikeEachOtherTotal(userCode));
            params.put("page", page);
        }

        final List<UserWlmEntity> matchList = this.getSqlSession().selectList(this.getStatement("queryLikeEachOtherList"), params);
        if (CollectionUtils.isEmpty(matchList)) {
            return null;
        }

        final UserEntity currentUser = userDao.selectByUserCode(userCode);
        return matchList.parallelStream().map(o -> {
            int nearby = 0;
            if (Objects.nonNull(currentUser.getGpsPrivilege()) && currentUser.getGpsPrivilege() == 1) {
                if (UserType.CHATTER_MODEL.getType() == o.getUserType()) {
                    nearby = 1;
                }
            }
            o.setNewFlag(nearby);
            return o;
        }).collect(Collectors.toList());

    }

    @Override
    public List<UserWlmEntity> queryLikeMeForModel(Page page) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("offset", page.getStartIndex());
        params.put("limit", page.getPageSize());

        return this.getSqlSession().selectList(this.getStatement("queryLikeMeForModel"), params);
    }

    @Override
    public int queryLikeMeForModelTotal() {
        Map<String, Object> params = Maps.newHashMap();
        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryLikeMeForModelTotal"), params);
    }

    public int queryLikeEachOtherTotal(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("deleted", 0);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("queryLikeEachOtherTotal"), params);
    }

    /**
     * 用于指定时间内收到like back数量查询
     */
    @Override
    public int receivedLikeBackNum(String userCode, LikeEnum likeType, OnlineStatus onlineStatus, DateTime dateTime) {
        Map<String, Object> params = Maps.newHashMap();
        if (Objects.nonNull(likeType)) {
            params.put("wlmType", likeType.getType());
        }
        params.put("deleted", 0);
        params.put("userCodeWlm", userCode);
        params.put("likesBackTime", dateTime);
        if (Objects.nonNull(onlineStatus)) {
            params.put("online", onlineStatus.getType());
        }
        return (Integer) this.getSqlSession().selectOne(this.getStatement("receivedLikeBackNum"), params);
    }

    @Override
    public UserWlmEntity receivedMaxLikeBackTime(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("receivedMaxLikeBackTime"), params);
    }

    @Override
    public List<UserWlmEntity> queryConnectRecord(String userCode, String userCodeWlm) {
        Map<String, String> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeWlm", userCodeWlm);
        return getSqlSession().selectList(this.getStatement("queryConnectRecord"), params);
    }

    @Override
    public List<FilterOnlineUserCustomerDBO> listOnlineUserChats(List<String> customerCodes, List<String> modelCodes) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("customerCodes", customerCodes);
        params.put("modelCodes", modelCodes);
        return getSqlSession().selectList(this.getStatement("listOnlineUserChatsV2"), params);
    }

    @Override
    public List<UserWlmEntity> queryFlashchatRecords(String userCode) {
        Map<String, String> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return getSqlSession().selectList(this.getStatement("queryFlashchatRecords"), params);
    }

    @Override
    public List<UserWlmEntity> getTodayWlmRecords(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectList(this.getStatement("getTodayWlmRecords"), params);
    }

    @Override
    public List<UserWlmEntity> getTodayRecordsByWmlCode(String wmlUserCode, List<Integer> types) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", wmlUserCode);
        params.put("types", types);
        return this.getSqlSession().selectList(this.getStatement("getTodayRecordsByWmlCode"), params);
    }

    @Override
    public List<UserWlmEntity> getRecordsByWmlCodeV2(String wmlUserCode, Boolean isToday) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wmlUserCode", wmlUserCode);
        params.put("isToday", isToday);
        return this.getSqlSession().selectList(this.getStatement("getRecordsByWmlCodeV2"), params);
    }

    @Override
    public List<UserWlmEntity> getWlmRecordsV2(String userCode, Boolean isToday) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("isToday", isToday);
        return this.getSqlSession().selectList(this.getStatement("getWlmRecordsV2"), params);
    }

    @Override
    public int removeOverEffectiveRecord(String userCode, String userCodeWlm, LikeEnum likeEnum) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeWlm", userCodeWlm);
        params.put("wlmType", likeEnum.getType());
        return this.getSqlSession().delete(this.getStatement("removeOverEffectiveRecord"), params);
    }

    @Override
    public UserWlmEntity getRelation(String fromUser, String toUser) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("fromUser", fromUser);
        params.put("toUser", toUser);
        return this.getSqlSession().selectOne(this.getStatement("getRelation"), params);
    }

    @Override
    public void clearLikeExpiredRelation(Integer expiredMins) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("expiredMins", expiredMins);
        this.getSqlSession().update(this.getStatement("clearLikeExpiredRelation"), params);
    }

    @Override
    public void clearWLMExpiredRelation(Integer expiredMins) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("expiredMins", expiredMins);
        this.getSqlSession().update(this.getStatement("clearWLMExpiredRelation"), params);
    }

    @Override
    public void clearPassExpiredRelation(Integer expiredMins) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("expiredMins", expiredMins);
        this.getSqlSession().update(this.getStatement("clearPassExpiredRelation"), params);
    }

    @Override
    public void clearWlmButNotMatchExpiredRelation(Integer expiredMins) {

        Map<String, Object> params = Maps.newHashMap();
        params.put("expiredMins", expiredMins);
        this.getSqlSession().update(this.getStatement("clearWlmButNotMatchExpiredRelation"), expiredMins);
    }

    @Override
    public void clearDislikeExpiredRelation(Integer expiredMins) {

        Map<String, Object> params = Maps.newHashMap();
        params.put("expiredMins", expiredMins);
        this.getSqlSession().update(this.getStatement("clearDislikeExpiredRelation"), expiredMins);
    }

    @Override
    public UserWlmEntity getLastWlmRecord(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("getLastWlmRecord"), params);
    }

    @Override
    public Integer getCountByWmlCode(List<Integer> wlmTypes, String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wmlUserCode", userCode);
        params.put("wlmTypes", wlmTypes);
        params.put("isToday", true);
        return this.getSqlSession().selectOne(this.getStatement("getCountByWmlCode"), params);
    }

    @Override
    public Integer getCountByLikesCode(List<Integer> wlmTypes, String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wmlUserCode", userCode);
        params.put("wlmTypes", wlmTypes);
        params.put("isToday", true);
        return this.getSqlSession().selectOne(this.getStatement("getCountByLikesCode"), params);
    }

    @Override
    public UserWlmEntity getLastLKRecord(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("wmlUserCode", userCode);
        return this.getSqlSession().selectOne(this.getStatement("getLastLKRecord"), params);
    }

    @Override
    public int getBlackCount(String userCode, String userCodeWlm) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeWlm", userCodeWlm);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getBlackCount"), params);
    }

    @Override
    public int getMatchCount(String userCode, String userCodeWlm) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("userCodeWlm", userCodeWlm);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getMatchCount"), params);
    }

    /**
     * 只推送：2024-03-11 00:00:00 之后注册的用户数据
     */
    @Override
    public List<UserAfDBO> listConnectUsersLatestDayFromAppflyers() {
        return this.getSqlSession().selectList(this.getStatement("listConnectUsersLatestDayFromAppflyers"));
    }

    @Override
    public int getLikeCount(String userCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getLikeCount"), params);
    }

    @Override
    public int getConnectionCount(List<String> userCodes,Date contactTime) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("userCodes", userCodes);
        params.put("contactTime", contactTime);
        return (Integer) this.getSqlSession().selectOne(this.getStatement("getConnectionCount"), params);
    }

    @Override
    public List<ModelUserSendMsgNumDBO> listOnlineUserChats(String modelUserCode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("modelUserCode", modelUserCode);
        return this.getSqlSession().selectList(this.getStatement("listOnlineUserChats"), params);
    }

    @Override
    public List<UserWlmEntity> listCustomerLbModel(String customerCode, List<String> modelCodes) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("customerCode", customerCode);
        params.put("modelCodes", modelCodes);
        return this.getSqlSession().selectList(this.getStatement("listCustomerLbModel"), params);
    }

    @Override
    public List<UserWlmEntity> listUserWlmList(List<String> userCodes, String userCode) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("list", userCodes);
        return this.getSqlSession().selectList(this.getStatement("listUserWlmList"), params);
    }

    @Override
    public List<UserWlmEntity> listContactRelationByFlashchat() {
        return this.getSqlSession().selectList(this.getStatement("listContactRelationByFlashchat"));
    }
}